package com.thti.mecool.client.model.biz;

import android.content.Context;
import android.database.Cursor;

import com.thti.mecool.client.base.BaseActivity;
import com.thti.mecool.client.base.HandlerConstant;
import com.thti.mecool.client.db.DataBase;
import com.thti.mecool.client.model.po.TaskManage;
import com.thti.mecool.client.model.po.TaskStep;
import com.thti.mecool.client.util.TimeUtil;

import java.util.*;

public class TaskStepBizImpl {
	private DataBase dataBase;
	
	
	public TaskStepBizImpl(Context context) {
		dataBase = new DataBase(context);
	}

	public void save(TaskStep taskStep) {
		String sqlString = "insert into taskStep(planId,projectId,taskId,stepId,parentId,isType,actionId,name,isFinsh) values(?,?,?,?,?,?,?,?,0)";
		dataBase.execObject(
				sqlString,
				new Object[]{taskStep.getPlanId(),taskStep.getProjectId(),taskStep.getTaskId(),taskStep.getStepId(),
						taskStep.getParentId(),taskStep.getIsType(),taskStep.getActionId(),taskStep.getName()});
		dataBase.close();
	}
	
	@SuppressWarnings("unchecked")
	public List<TaskStep> findByjob(String planId,String projectId,String taskId,int isType){
		String sql="select * from taskStep where planId=? and projectId=? and taskId=? and isType=?";
		List<TaskStep> list=dataBase.findAll(TaskStep.class, sql, new String[]{planId,projectId,taskId,String.valueOf(isType)});
		dataBase.close();
		return list;
	}
	
	public String findStepData(String planId,String projectId,String taskId,String parentId,String stepId){
		String sql="select data from taskStep where planId=? and projectId=? and taskId=? and parentId=? and stepId=?";
		Cursor cursor=dataBase.getWritableDatabase().rawQuery(sql, new String[]{planId,projectId,taskId,parentId,stepId});
		while(cursor.moveToNext()){
			String data=cursor.getString(0);
			dataBase.close();
			cursor.close();
			return data;
		}
		return null;
	}
	
	@SuppressWarnings("unchecked")
	public List<TaskStep> findByStep(String planId,String projectId,String parentId,String taskId){
		String sql="select * from taskStep where planId=? and projectId=? and isType<>0 and parentId=? and taskId=?";
		List<TaskStep> list=dataBase.findAll(TaskStep.class, sql, new String[]{planId,projectId,parentId,taskId});
		dataBase.close();
		return list;
	}

	@SuppressWarnings("unchecked")
	public List<TaskStep> findByMulStep(String planId,String projectId,String parentId,String taskId){
		String sql="select * from taskStep where planId=? and projectId=? and actionId=4 and isType<>0 and parentId=? and taskId=?";
		List<TaskStep> list=dataBase.findAll(TaskStep.class, sql, new String[]{planId,projectId,parentId,taskId});
		dataBase.close();
		return list;
	}
	public boolean findStepFinish(String planId,String taskId,String projectId,String stepId){
		String sql="select isFinsh from taskStep where planId=? AND taskId=? AND projectId=? AND stepId=?";
		Cursor cursor=dataBase.getWritableDatabase().rawQuery(sql, new String[]{planId,taskId,projectId,stepId});
		boolean finishOK = false;
		if(cursor.moveToNext()){
			System.out.println(cursor.getInt(0));
			finishOK=cursor.getInt(0)==1;
			System.out.println(finishOK);
		}
		dataBase.close();
		return finishOK;
	}
	
	public List<TaskStep> findBelowStep(String planId,String projectId,String stepId,String taskId){
		String sql="select * from taskStep where planId=? and projectId=? and parentId=(select stepId from taskStep where stepId=?) and taskId=?";
		List<TaskStep> list=dataBase.findAll(TaskStep.class, sql, new String[]{planId,projectId,stepId,taskId});
		return list!=null?list:new ArrayList<TaskStep>();
	}
	
	public void updateStep(List<Map<String,Object[]>> list){
		dataBase.getWritableDatabase().beginTransaction();//开始事务
		if(list!=null && list.size()>0){
			try {
				for (int i = 0; i < list.size(); i++) {
					if(list.get(i).containsKey(HandlerConstant.PLAN_UPDATE_SQL)){
						dataBase.getWritableDatabase().execSQL(HandlerConstant.PLAN_UPDATE_SQL,list.get(i).get(HandlerConstant.PLAN_UPDATE_SQL));
					}else if(list.get(i).containsKey(HandlerConstant.UPDATE_STEP_SQL)){
						dataBase.getWritableDatabase().execSQL(HandlerConstant.UPDATE_STEP_SQL,list.get(i).get(HandlerConstant.UPDATE_STEP_SQL));
					}else
					if(list.get(i).containsKey(HandlerConstant.TASK_STEP_UPDATE_DATA_SQL)){
						dataBase.getWritableDatabase().execSQL(HandlerConstant.TASK_STEP_UPDATE_DATA_SQL,list.get(i).get(HandlerConstant.TASK_STEP_UPDATE_DATA_SQL));
					}else if(list.get(i).containsKey("update taskStep set data=? where planId=? and projectId=?  and stepId=? and taskId=?")){
						System.out.println("mul   truemul   truemul   truemul   truemul   truemul   truemul   true");
						dataBase.getWritableDatabase().execSQL("update taskStep set data=? where planId=? and projectId=?  and stepId=? and taskId=?",list.get(i).get("update taskStep set data=? where planId=? and projectId=?  and stepId=? and taskId=?"));
					}
				}
				dataBase.getWritableDatabase().setTransactionSuccessful();//调用此方法会在执行到endTransaction() 时提交当前事务，如果不调用此方法会回滚事务
			} finally {
				dataBase.getWritableDatabase().endTransaction();//由事务的标志决定是提交事务，还是回滚事务
			}
			
		}
		dataBase.getWritableDatabase().close(); 
	}
	public void updateData(TaskManage taskManage){
		//update taskStep set data=?,images=?,isUpload=0,latitude=?,longitude=?,createTime=? where planId=? and projectId=?  and stepId=? and taskId=?";
	   	dataBase.getWritableDatabase().execSQL(HandlerConstant.UPDATE_STEP_SQL,new Object[]{taskManage.getData(),taskManage.getImages(),taskManage.getLatitude(),taskManage.getLongitude(),TimeUtil.getCurLong(BaseActivity.getCurrentUser().getCurrentServerTime()).getTime(),taskManage.getPlanId(),taskManage.getProjectId(),taskManage.getStepId(),taskManage.getTaskId()});
	   	dataBase.close();
	}
	@SuppressWarnings("unchecked")
	public List<TaskStep> findFinishSteps(String projectId,String taskId,String parentId){
		String sql = "select * FROM taskStep WHERE isFinsh=1 AND projectId=? AND actionId!=9 AND taskId=? AND parentId=? AND isOver=2";
		List<TaskStep> taskSteps=dataBase.findAll(TaskStep.class, sql, new String[]{projectId,taskId,parentId});
		return taskSteps;
	}
	
	public void clearStep(String planId,String taskId,String parentId,String projectId){
		ArrayList<String> stepList=new ArrayList<String>();
		findStepId(planId, projectId, taskId, parentId, stepList);
		ArrayList<HashMap<String, String[]>> args=new ArrayList<HashMap<String,String[]>>();
		for(String s:stepList){
			HashMap<String, String[]> arg=new HashMap<String, String[]>();
			//isFinsh=? where planId=? and projectId=?  and stepId=? and taskId=?"
			arg.put(HandlerConstant.PLAN_UPDATE_SQL, new String[]{"0",planId,projectId,s,taskId});
			arg.put(HandlerConstant.TASK_STEP_UPDATE_DATA_SQL, new String[]{null,planId,projectId,s,taskId});
			args.add(arg);
		}
		dataBase.execTransaction(args);
	}
	
	public ArrayList<String> findStepId(String planId,String projectId,String taskId,String parentId,ArrayList<String> list){
		String sql = "select stepId FROM taskStep WHERE planId=? AND projectId=? AND taskId=? AND parentId=?";
		Cursor cursor=dataBase.getWritableDatabase().rawQuery(sql, new String[]{planId,projectId,taskId,parentId});
		while(cursor.moveToNext()){
			String s=cursor.getString(0);
			list.add(s);
			findStepId(planId, projectId, taskId, s,list);
		}
		return list;
	}
	
	@SuppressWarnings("unchecked")
	public List<TaskStep> findFinishSteps(String projectId,String taskId){
		String sql = "select * FROM taskStep WHERE isFinsh=1 and actionId!=9 AND projectId=? AND taskId=? AND parentId=taskId";
		List<TaskStep> taskSteps=dataBase.findAll(TaskStep.class, sql, new String[]{projectId,taskId});
		return taskSteps;
	}
	public void delete(String planId){
		String sql="delete  from taskStep where planId=?";
		dataBase.execObject(sql, new Object[]{planId});
		dataBase.close();
	}
}
